import json
import time


from django.conf import settings
from django.core.paginator import Paginator, EmptyPage
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.utils import timezone
from django.views import View

from aijia_rental.utils.qiniuyun.qiniu_storage import storage
from aijia_rental.utils.views import LoginRequiredJSONMixin
from lina.models import HouseImage, House
from lishiliang.models import User


class UserCenterView(LoginRequiredJSONMixin,View):

    def get(self,request):
        '''用户中心页面'''
        #获取当前的用户对象
        user = request.user

        try:
            #查找数据库
            users = User.objects.get(mobile=user.mobile)
            user_dict = {}
            # 前端需要的数据

            url = settings.QNY_URL
            avatar = url+str(users.avatar)
            data = {'avatar_url':avatar,
                                 'create_time':str(users.date_joined),
                                 'mobile':users.mobile,
                                 'name':users.username,
                                 'user_id':users.id}

        except Exception as e:
            return JsonResponse({"errno": "4001",
                                "errmsg": "数据库查询错误"})


        return JsonResponse({'data':data,
                             'errmsg':'OK',
                             'errno':'0'})


class UpdateUesrNameView(LoginRequiredJSONMixin, View):
# class UpdateUesrNameView(View):

    def put(self,request):

        '''修改用户名'''
        user = request.user
        # 接收参数
        dict = json.loads(request.body.decode())
        # 提取参数
        username = dict['name']
        # 验证参数
        if not username:
            return JsonResponse({"errno": "4103",
                                "errmsg": "参数错误"})

        try:
            # 修改数据库
            User.objects.filter(mobile=user.mobile).update(username=username)
        except Exception as e:
            return JsonResponse({"errno": "4001",
                                "errmsg": "数据库查询错误"})

        return JsonResponse({
                            "errno": "0",
                            "errmsg": "修改成功"})



class HeadImageView(LoginRequiredJSONMixin,View):


    def post(self,request):
        '''上传头像'''

        user = request.user
        #接受文件
        image_path = request.FILES.get('avatar')
        # 提取文件
        # image_path = image_path.name
        im_path = image_path.read()
        # print(im_path)
        # 验证文件
        if not im_path:
            return JsonResponse({"errno": "4103",
                                "errmsg": "参数错误"})
        try:
            # 获取数据库对象修改添加图片地址到数据库
            save_path = User.objects.get(mobile=user.mobile)
            # 读取文件并且上传到七牛云返回相应路径
            # with open('/home/ubuntu/Desktop/' + image_path, "rb") as f:
            #     path = storage(f.read())
            path = storage(im_path)
        except Exception as e:
            return JsonResponse({"errno": "4001",
                                 "errmsg": "数据库查询错误"})

        # 七牛云域名地址拼接路径
        url = settings.QNY_URL
        result = url+path
        # 判断 上传到七牛云是否成功
        if not path :
            return JsonResponse({"errno": "4001",

                                 "errmsg": "七牛云上传失败"})
        # 图片路径添加到数据库
        else:
            try:
                save_path.avatar = path
                save_path.save()
            except Exception as e:
                return JsonResponse({"errno": "4001",
                                     "errmsg": "图片添加数据库错误"})
        re_dict = {'avatar_url':result}
        # re_dict['avatar_url'] = path

        return JsonResponse({'data':re_dict,
                             "errno": "0",
                             "errmsg": "头像上传成功"})


class HomeImageView(LoginRequiredJSONMixin, View):

    def post(self, request, house_id):

        # 接受文件
        image_path = request.FILES.get('house_image')
        # 提取文件
        # image_path = image_path.name
        img_path = image_path.read()
        # 验证文件
        if not img_path:
            return JsonResponse({"errno": "4103",
                                 "errmsg": "参数错误"})
        try:
            pimage = House.objects.get(id=house_id)
            image = HouseImage(house_id=house_id)
            # 读取文件并且上传到七牛云返回相应路径
            # with open('/home/ubuntu/Desktop/' + image_path, "rb") as f:
            #     path = storage(f.read())
            path = storage(img_path)
        except Exception as e:
            return JsonResponse({"errno": "4001",
                                 "errmsg": "添加数据库错误"})

        # 七牛云域名地址拼接路径
        url = settings.QNY_URL
        result = url + path
        # 判断 上传到七牛云是否成功
        if not path:
            return JsonResponse({"errno": "4001",

                                 "errmsg": "七牛云上传失败"})
        # 图片路径添加到数据库
        else:
            try:
                img = pimage.index_image_url

                if not img :
                    pimage.index_image_url = path
                    pimage.save()

                else:
                    image.house_id = house_id
                    image.url = path
                    image.save()
            except Exception as e:
                return JsonResponse({"errno": "4001",
                                     "errmsg": "图片添加数据库错误"})
        re_dict = {}
        re_dict['url'] = result

        return JsonResponse({'data': re_dict,
                             "errno": "0",
                             "errmsg": "图片上传成功"})











